package com.xca.is.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.Util;
import com.xca.is.R;
import com.xca.is.dataIntent.FragmentIsDestory;
import com.xca.is.dataIntent.FragmentIsDestroy2;
import com.xca.is.dataIntent.VideoPlayModel;
import com.xca.is.views.MainActivity;

public class Video2Fragment extends Fragment implements FragmentIsDestory {
    private String url ;
    private SimpleExoPlayer player ;
    private PlayerView playerView ;

    private ImageView imageView ;
    private VideoFragment fragment ;

    public Video2Fragment(VideoFragment fragment , String url) {
        this.fragment = fragment ;
        this.url = url;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_video2, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
                playerView = view.findViewById(R.id.player_view);
                imageView = view.findViewById(R.id.video_stop);

                imageView.setVisibility(View.GONE);
                fragment.setData(this);
                player = new SimpleExoPlayer.Builder(getActivity()).build();
                playerView.setPlayer(player);


                MediaItem mediaItem = MediaItem.fromUri(url) ;
                player.setMediaItem(mediaItem);
                player.prepare();
                player.setPlayWhenReady(true);

                setViewClickListener();
                setLooping();
                addPlayerListener();
                setUserVisibleHint(true);
    }


    //设置屏幕点击暂停功能
    private void setViewClickListener() {
        playerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                togglePlay();
            }
        });
    }
    //播放暂停功能
    private void togglePlay() {
        if (player.isPlaying()){
            player.pause();
            imageView.setVisibility(View.VISIBLE);
        }else {
            player.play();
            imageView.setVisibility(View.GONE);
        }
    }
    //循环播放功能
    private void setLooping() {
        player.addListener(new Player.Listener() {
            @Override
            public void onPlaybackStateChanged(int playbackState) {
               if (playbackState == Player.STATE_ENDED){
                   player.seekTo(0);
                   if (player.isPlaying()){
                       player.play();
                   }
               }
            }
        });
    }
    //监听生命周期，只有当前页面才播放视频
    private void addPlayerListener() {
        getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event) {
                switch (event){

                    case ON_RESUME:
                        player.setPlayWhenReady(true);
                        imageView.setVisibility(View.GONE);
                        break;
                    case ON_PAUSE:
                        player.setPlayWhenReady(false);
                        break;
                    case ON_DESTROY:
                        Log.d("视频走到此处", "onStateChanged: ");
                        player.release();
                        break;
                }
            }
        });
    }
    //监听可见性，只有当前页面才播放视频
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            player.play();
            imageView.setVisibility(View.GONE);
        }else {
            player.pause();
            imageView.setVisibility(View.VISIBLE);
        }
    }

    //释放播放器
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (player != null) {
            player.stop();
            player.release();
            player = null;
            playerView.setPlayer(null);
            Log.d("视频播放页面已销毁", "视频播放页面已销毁");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("视频播放页面已销毁", "视频播放页面已销毁");
        if(player != null){
            player.stop();
            player.pause();
            player.release();
        }
    }

    //恢复播放
    @Override
    public void onStart() {
        super.onStart();
        if (Util.SDK_INT>23){
            player.setPlayWhenReady(true);
        }
    }
    //暂停播放
    @Override
    public void onStop() {
        super.onStop();
        if (Util.SDK_INT<=23 || player.getPlayWhenReady()){
            player.setPlayWhenReady(false);
            player.seekTo(0);
            Log.d("视频播放页面已停止", "视频播放页面已停止");
        }
    }

    //隐藏页面,
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            player.play();
        }else {
            Log.d("视频播放页面已不可见", "视频播放页面已不可见");
            player.setPlayWhenReady(false);
            player.pause();
            player.release();
        }
    }

    @Override
    public void isDestory(Boolean bool) {
        if (bool){
            Log.d("VideoFragment已经销毁", "VideoFragment已经销毁");
            player.pause();
        }
    }

}